<?php


namespace app\badmin\controller;


use app\badmin\model\Company;
use app\common\controller\BadminController;
use app\badmin\model\Region as RegionModel;
use app\badmin\model\HouseTableConfig as HouseTableConfigModel;
use think\Exception;
use think\facade\Request;

class HouseTableConfig extends BadminController {

    /**
     * 添加获取
     * @return false|string
     */
    public function addGet() {
        try {
            $params = $this->checkParams('');
            if ($params === false) return $this->resErr();
            $company = Company::getOne([['id', '=', $params['company_id']]], 'city_code');
            $city_code = explode(',', $company['city_code']);
            foreach ($city_code as $k => $v) {
                $city_code_ = explode('-', $v);
                $city_code[$k] = $city_code_[0];
            }
            $rank2 = RegionModel::getDatas([['city_code', 'in', $city_code]], 'name,id,city_code');//城市列表
            $type = HouseTableConfigModel::TYPE;
            return $this->resData('success', compact('rank2', 'type'));
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 添加提交
     * @return false|string
     */
    public function addPost() {
        try {
            $params = $this->checkParams('key,level');
            if ($params === false) return $this->resErr();
            $res = HouseTableConfigModel::add($params);
            if (!$res) return $this->resErr();
            else return $this->resData('添加成功', []);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 上传预览
     */
    public function clickPreview1() {
        try {
            $params = $this->checkParams('city_code,city_name,type');
            if ($params === false) return $this->resErr();
            $res = HouseTableConfigModel::clickPreview($params);
            if (!$res) return $this->resErr();
            else return $this->resData('success', ['key' => $res]);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 上传预览
     */
    public function clickPreview2() {
        try {
            $params = $this->checkParams('key');
            if ($params === false) return $this->resErr();
            $body = HouseTableConfigModel::getRespCacheKey($params);
            $request = HouseTableConfigModel::getRequestCacheKey($params);
            $head = '最新优质法拍房' . ' (' . date('Y-m-d') . ')';
            $title = $request['city_name'] . '司法拍卖房 不限购 可贷款';
            $logo = Company::where('id', $params['company_id'])->value('logo');
            return $this->resData('success', compact('head', 'title', 'logo', 'body'));
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 列表
     * @return false|string
     */
    public function getList() {
        try {
            $params = $this->checkParams('');
            if ($params === false) return $this->resErr();
            $data = HouseTableConfigModel::getList($params);
            return $this->resData('success', $data);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 删除
     * @return false|string
     */
    public function del() {
        try {
            $params = $this->checkParams('id');
            if ($params === false) return $this->resErr();
            $res = HouseTableConfigModel::del($params);
            if (!$res) return $this->resErr();
            else return $this->resData('删除成功', []);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 上传
     * @return false|string
     */
    public function upload() {
        try {
            $params = $this->checkParams('');
            if ($params === false) return $this->resErr();
            $file = request()->file('img');
            $file_header = config('global.file_dir');
            $file_domain = config('global.file_domain');
            $info = $file->validate(['size' => 10242880, 'ext' => 'jpg,png,gif,jpeg'])->move($file_header . 'house_table/');
            if ($info) {
                $save_file = $file_domain . 'house_table/' . $info->getSaveName();
                return $this->resData('success', ['url' => $save_file]);
            } else {
                // 上传失败获取错误信息
                echo $file->getError();
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

}